package com.avic.rong.utils;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * created by xtj on 2017/12/18 9:28.
 */

public abstract class SharedPreferencesBase {
    public abstract SharedPreferences getSharedPreferences();

    protected void removeKey(String key) {
        Editor editor = getSharedPreferences().edit();
        editor.remove(key);
        editor.commit();
    }

    /* ----- Boolean ----- */
    protected void addBoolean(String key, Boolean value) {
        Editor editor = getSharedPreferences().edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    protected boolean getBoolean(String key, boolean defValue) {
        return getSharedPreferences().getBoolean(key, defValue);
    }

    protected boolean getBooleanWithDefaultValueFalse(String key) {
        return getSharedPreferences().getBoolean(key, false);
    }

    protected boolean getBooleanWithDefaultValueTrue(String key) {
        return getSharedPreferences().getBoolean(key, true);
    }

    /* ----- String ----- */
    protected void addString(String key, String value) {
        Editor editor = getSharedPreferences().edit();
        editor.putString(key, value);
        editor.commit();
    }


    protected String getString(String key, String defValue) {
        return getSharedPreferences().getString(key, defValue);
    }

    protected String getStringWithDefaultValueNull(String key) {
        return getSharedPreferences().getString(key, null);
    }

    protected String getStringWithDefaultValueEmpty(String key) {
        return getSharedPreferences().getString(key, "");
    }

    protected String getStringWithDefaultValue0(String key) {
        return getSharedPreferences().getString(key, "0");
    }

    protected String getStringWithDefaultValue1(String key) {
        return getSharedPreferences().getString(key, "1");
    }

    protected String getStringWithDefaultValueTrue(String key) {
        return getSharedPreferences().getString(key, "true");
    }

    /* ----- int ----- */
    protected void addInt(String key, int value) {
        Editor editor = getSharedPreferences().edit();
        editor.putInt(key, value);
        editor.commit();
    }

    /* ----- long ----- */
    protected void addLong(String key, long value) {
        Editor editor = getSharedPreferences().edit();
        editor.putLong(key, value);
        editor.commit();
    }

    protected int getIntWithDefaultValue0(String key) {
        return getSharedPreferences().getInt(key, 0);
    }

    protected int getInt(String key, int defValue) {
        return getSharedPreferences().getInt(key, defValue);
    }

    protected long getLong(String key, long defValue) {
        return getSharedPreferences().getLong(key, defValue);
    }

    protected long getLongWithString(String key, long defValue) {
        String str = getString(key, "0");
        try {
            long value = Long.parseLong(str);
            return value;
        } catch (Exception e) {
            return defValue;
        }
    }

}
